<script setup lang="ts">
import CustomNavbar from "@/pages/index/componets/CustomNavbar.vue";
import CategoryPanel from "@/pages/index/componets/CategoryPanel.vue";
import HotPanel from "@/pages/index/componets/HotPanel.vue";
import { getHomeBannerApi, getHomeCategoryAPI, getHotItemAPI } from "@/services/home";
import { ref } from "vue";
import type { BannerItem, CategoryItem, HotItem } from "@/types/home";
import { onLoad } from "@dcloudio/uni-app";
import type { XtxGuessInstance } from "@/types/component";
import PageSkeleton from '@/pages/index/componets/PageSkeleton.vue'


const bannerList = ref<BannerItem[]>([])
const categoryList = ref<CategoryItem[]>([])
const hotItemList = ref<HotItem[]>([])

const getHomeBannerData = async () => {
  const res = await getHomeBannerApi()
  bannerList.value = res.result
}

const getCategoryItemData = async () => {
  const res = await getHomeCategoryAPI()
  categoryList.value = res.result;

}

const getHotItemData = async () => {
  const res = await getHotItemAPI()
  hotItemList.value = res.result
}

const guessRef = ref<XtxGuessInstance>()
const onScrolltolower = () => {
  guessRef.value?.getMore()
}

const isLoading = ref(true)
onLoad(async () => {
  await Promise.all([getHomeBannerData(), getCategoryItemData(), getHotItemData()])
  isLoading.value = false;
})


const refreshStatus = ref(false)
const refresh = async () => {
  // 刷新触发
  refreshStatus.value = true
  // 并发调用多个请求
  await Promise.all([getHomeBannerData(), getCategoryItemData(), getHotItemData()])
  // 请求调用完毕后关闭刷新样式
  refreshStatus.value = false
}
</script>

<template>
  <CustomNavbar />
  <scroll-view refresher-enabled @refresherrefresh="refresh" :refresher-triggered="refreshStatus" scroll-y
    class="scroll-view" @scrolltolower="onScrolltolower">
    <!-- 骨架图 -->
    <PageSkeleton v-if="isLoading" />
    <template v-else>
      <XtxSwiper :list="bannerList" />
      <CategoryPanel :list="categoryList" />
      <HotPanel :list="hotItemList" />
      <XtxGuess ref="guessRef" />
    </template>
  </scroll-view>
</template>

<style lang="scss">
page {
  background-color: #f7f7f7;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.scroll-view {
  flex: 1;
}
</style>
